package com.tjec.project.portal.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tjec.common.constant.Constants;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.security.LoginUser;
import com.tjec.framework.security.service.SysLoginService;
import com.tjec.framework.security.service.TokenService;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.project.system.domain.SsoUserVo;
import com.tjec.project.system.domain.SysUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 门户Controller
 * 
 * @author pangyongfeng
 * @date 2021-01-12
 */
@Api(value = "门户接口", tags = {"门接口"})
@RestController
@RequestMapping("/portal/foreign")
public class BmsPortalController extends BaseController{
	
	@Autowired
    private SysLoginService loginService;
	
	@Autowired
	 private TokenService tokenService;

    @Value("${ECURL.proUrl}")
    private String ecUrl;
	
	 /**
     * 新增项目流程
	 * @throws IOException 
     */
    @ApiOperation("新增项目流程")
    @Log(title = "项目流程", businessType = BusinessType.INSERT)
    @GetMapping("/ssoLogin/{token}")
    public void ssoLogin(@PathVariable String token,HttpServletResponse resp) throws IOException
    {
    	System.out.println("单点登陆进行中。。。。。。。");
    	
    	resp.sendRedirect(ecUrl+"/ssoLogin?token="+token);
    }
    
    /**
     * 门户单点登录
     *
     * @param jobNo 工号
     * @return 结果
     */
    @PostMapping("/portalSsoLogin")
    public AjaxResult portalSsoLogin(String jobNo)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.portalSsoLogin(jobNo);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    
    @PostMapping("/decipherToken")
    @ApiOperation(value = "解密token", notes = "解密token")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "系统密钥（各系统不同）", dataType = "String", paramType = "query",required = true)
    })
    @ResponseBody
    public Map<String, Object> decipherToken(@RequestParam(required = true) String token) {
        Map<String, Object> map = new ConcurrentHashMap<>(4);
        /**
         * 正式逻辑--开始
         */
        LoginUser userFromToken = tokenService.getUserFromToken(token);
        if (userFromToken==null){
            map.put("code", -1);
            map.put("msg", "用户已失效");
            return map;
        }
        SysUser loginUser = userFromToken.getUser();
        try {
            SsoUserVo ssoUserVo = new SsoUserVo();
            ssoUserVo.setUid(loginUser.getJobNo());
            ssoUserVo.setUname(loginUser.getNickName());
            map.put("code", 0);
            map.put("msg", "用户信息解析成功");
            map.put("data", ssoUserVo);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        /**
         * 正式逻辑--结束
         */
        return map;
    }
	
}
